package com.hazardSoft.eStore.bussines;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.hazardSoft.eStore.common.exception.SystemException;
import com.hazardSoft.eStore.entities.Product;
import com.hazardSoft.eStore.utils.PathConstants;
import com.hazardSoft.eStore.utils.QueryConstants;

public class ProductsBO {

	private static final String CLASS_NAME = PathConstants.BUSSINES_PATH+ProductsBO.class.getSimpleName();
	
	public Product insertProduct(Product product, EntityManager entityManager) throws SystemException {
		if (this.searchProductbyName(product, entityManager) == null) {
			if (product.getId() == null) {
				product.setId(0);
			}
			
			product.setName(product.getName().toLowerCase());
			entityManager.persist(product);
			
			return this.searchProductbyName(product, entityManager);
		}
		
		return null;
	}

	
	public List<Product> listProducts(EntityManager entityManager) throws SystemException {
		
		Query query  = entityManager.createNamedQuery(QueryConstants.QUERY_LIST_PRODUCTS);
		
		return query.getResultList();
	}

	
	public boolean removeProduct(Product product, EntityManager entityManager) throws SystemException {
		if (product == null) {
			throw new SystemException(new Exception("null product"), CLASS_NAME, "removeProduct");
		}
		if (!(product.getId() == null)) {
			entityManager.remove(product);
			return true;
		} else {
			if (!(product.getName() == null)) {
				entityManager.remove(product);
				return true;
			} else {
				throw new SystemException(new Exception("null product.id or product.name"), CLASS_NAME, "removeProduct");
			}
		} 
	}

	
	public Product searchProductbyId(Product product, EntityManager entityManager) throws SystemException {
		if (product == null) {
			throw new SystemException(new Exception("null product"), CLASS_NAME, "searchProductById");
		}
		if (product.getId() == null) {
			throw new SystemException(new Exception("null product.id"), CLASS_NAME, "searchProductById");
		}
		
		return entityManager.find(Product.class, product.getId());
	}

	
	public Product updateProduct(Product product, EntityManager entityManager) throws SystemException {
		Product tempProduct = this.searchProductbyName(product, entityManager);
		if (!(tempProduct == null)) {
			if (!(product.getDescription() == null)) {
				tempProduct.setDescription(product.getDescription());
			}
			if (!(product.getCategory() == null)) {
				tempProduct.setCategory(product.getCategory());
			}
			if (!(product.getBrand() == null)) {
				tempProduct.setBrand(product.getBrand());
			}
			if (!(product.getCost() == null)) {
				tempProduct.setCost(product.getCost());
			}
			if (!(product.getBrand() == null)) {
				tempProduct.setBrand(product.getBrand());
			}
			tempProduct.setOffer(product.getOffer());
			tempProduct.setImage(product.getImage());
			
			return tempProduct;
		}
		return null;
	}
	
	public Product searchProductbyName(Product product, EntityManager entityManager) throws SystemException{
		if (product == null) {
			throw new SystemException(new Exception("null product"), CLASS_NAME, "searchProductByName");
		}
		if (product.getName() == null) {
			throw new SystemException(new Exception("null product.name"), CLASS_NAME, "searchProductByName");
		}
		
		Query query = entityManager.createNamedQuery(QueryConstants.QUERY_SEARCH_PRODUCT_BY_NAME);
		query.setParameter(1, product.getName().toLowerCase());
		
		List<Product> products = query.getResultList();
		
		if (products.size() > 0) {
			return products.get(0);
		}
		
		return null;
	}
	
	public List<Product> searchProducts(Product product, EntityManager entityManager) throws SystemException{
		if (product == null) {
			throw new SystemException(new Exception("null product"), CLASS_NAME, "searchProducts");
		}
		if (product.getName() == null) {
			throw new SystemException(new Exception("null product.name"), CLASS_NAME, "searchProducts");
		}
		
		String[] keyWords = product.getName().split(" ");
		
		String stringQuery = "FROM Product p WHERE ";
		
		int index = 0;
		for (String keyWord : keyWords) {
			if (index == keyWords.length-1) {
				stringQuery += "p.name LIKE '%"+keyWord+"%'";
			} else {
				stringQuery += "p.name LIKE '%"+keyWord+"%' OR ";
			}
			index++;
		}
		
		Query query = entityManager.createQuery(stringQuery);
		
		return query.getResultList();
	}

}
